const instance = require("../utils/instance");
const { useAgent } = require("../hooks");
const getSign4 = require("../utils/getSign4");

const kuaiApi = {
  // 获取快手二维码
  getKuaishouQrcode() {
    return instance({
      url: "https://id.kwaixiaodian.com/rest/c/infra/ks/qr/start",
      method: "post",
      data: {
        sid: "kuaishou.shop.b",
        channelType: "UNKNOWN",
        encryptHeaders: "",
      },
      headers: {
        "content-type": "application/x-www-form-urlencoded",
        origin: "https://login.kwaixiaodian.com",
        referer:
          "https://login.kwaixiaodian.com/?biz=zone&redirect_url=https%3A%2F%2Fs.kwaixiaodian.com%2Fzone%2Fhome",
        "user-Agent": useAgent(),
      },
    });
  },
  // 提交画画
  async submit({ cookie, data, agent }) {
    const __NS_hxfalcon = await getSign4("/api/task/submit", {}, data);
    console.log(__NS_hxfalcon);
    return instance({
      url: "https://klingai.kuaishou.com/api/task/submit",
      method: "post",
      data: data,
      params: {
        __NS_hxfalcon,
        caver: 2,
      },
      headers: {
        origin: "https://klingai.kuaishou.com",
        referer: "https://klingai.kuaishou.com/text-to-image/new",
        cookie,
        "user-agent": agent,
      },
    });
  },
  // 生成状态
  async status({ cookie, params, agent }) {
    const __NS_hxfalcon = await getSign4("/api/task/status", params);
    return instance({
      url: "https://klingai.kuaishou.com/api/task/status",
      method: "get",
      params: {
        ...params,
        __NS_hxfalcon,
        caver: 2,
      },
      headers: {
        origin: "https://klingai.kuaishou.com",
        referer: "https://klingai.kuaishou.com/text-to-image/new",
        cookie,
        "user-agent": agent,
      },
    });
  },
  // 检测是否登录
  checkCookieInvalid({ cookie }) {
    return instance({
      url: "https://s.kwaixiaodian.com/rest/app/tts/seller/login/info",
      method: "get",
      headers: {
        kpf: "PC_WEB",
        kpn: "",
        referer: "https://s.kwaixiaodian.com/zone/home",
        cookie,
        "user-agent": useAgent(),
      },
    });
  },
  // 刷新积分
  reward({ cookie }) {
    return instance({
      url: "https://api-app-cn.klingai.com/api/pay/reward",
      method: "get",
      headers: {
        referer: "https://app.klingai.com/",
        cookie,
      },
    });
  },
  // 获取积分
  pointAndTicket({ cookie }) {
    return instance({
      url: "https://api-app-cn.klingai.com/api/account/pointAndTicket",
      method: "get",
      headers: {
        referer: "https://api-app-cn.klingai.com/",
        cookie,
      },
    });
  },
};

module.exports = kuaiApi;
